diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-12-18 11:56:59 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-12-18 11:56:59 +0100 |
| commit | d592085dc0fec023dd9f3437d4c756d402ed8c8f (patch) | |
| tree | 9bba6873cefac197934ca41eed22b0b7054e3f60 /src/pages/article/[slug].tsx | |
| parent | 67581629ea87162e6d47b39b867b37a9a50754aa (diff) | |
fix(pages): use dynamic imports for the table of contents
The ToCWidget relies on Javascript to display the headings. If JS is
disabled in the browser, the widget is empty. For a better UX, we
should use dynamic imports. Now, the ToC is only displayed when JS is
enabled.
Diffstat (limited to 'src/pages/article/[slug].tsx')
| -rw-r--r-- | src/pages/article/[slug].tsx | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/src/pages/article/[slug].tsx b/src/pages/article/[slug].tsx index ecff692..9c7a156 100644 --- a/src/pages/article/[slug].tsx +++ b/src/pages/article/[slug].tsx @@ -1,6 +1,7 @@ /* eslint-disable max-statements */ import type { ParsedUrlQuery } from 'querystring'; import type { GetStaticPaths, GetStaticProps } from 'next'; +import dynamic from 'next/dynamic'; import Head from 'next/head'; import { useRouter } from 'next/router'; import { type FC, useCallback } from 'react'; @@ -16,7 +17,6 @@ import { PageFooter, PageComments, PageSidebar, - TocWidget, LoadingPage, LoadingPageComments, } from '../../components'; @@ -52,6 +52,13 @@ import { usePrism, } from '../../utils/hooks'; +const Toc = dynamic( + async () => import('../../components').then((mod) => mod.TocWidget), + { + ssr: false, + } +); + type ArticlePageProps = { data: { comments: WPComment[]; @@ -228,7 +235,7 @@ const Article: FC<Pick<ArticlePageProps, 'data'>> = ({ data }) => { }} /> <PageSidebar> - <TocWidget + <Toc heading={<Heading level={2}>{messages.tocTitle}</Heading>} tree={tree} /> @@ -291,7 +298,8 @@ export const getStaticProps: GetStaticProps<ArticlePageProps> = async ({ locale, params, }) => { - const post = await fetchPost((params as PostParams).slug); + const { slug } = params as PostParams; + const post = await fetchPost(slug); const comments = await fetchCommentsList({ first: post.commentCount ?? 1, where: { contentId: post.databaseId }, |
